package leo.gptBot.gpt;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import static dev.langchain4j.data.message.ChatMessageDeserializer.messagesFromJson;
import static dev.langchain4j.data.message.ChatMessageSerializer.messagesToJson;

import java.util.List;

/**
 * @author ：Leo
 * @date ：Created in 2024/7/23 14:12
 * @description：
 */
@Component
public class PersistentChatMemoryStore implements ChatMemoryStore {
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Override
    public List<ChatMessage> getMessages(Object o) {
        String json = redisTemplate.opsForValue().get(o.toString());
        return messagesFromJson(json);
    }

    @Override
    public void updateMessages(Object o, List<ChatMessage> list) {
        redisTemplate.opsForValue().set(o.toString(), messagesToJson(list));
    }

    @Override
    public void deleteMessages(Object o) {
        redisTemplate.delete(o.toString());
    }
}
